<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>如何在Ubuntu20.04上安装KVM |  朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-如何在Ubuntu20-04上安装KVM"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  如何在Ubuntu20.04上安装KVM
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/21/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20-04%E4%B8%8A%E5%AE%89%E8%A3%85KVM/" class="article-date">
  <time datetime="2023-04-21T11:16:19.000Z" itemprop="datePublished">2023-04-21</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Linux/">Linux</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.9k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">12 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>KVM(基于内核的虚拟机)是一个内置于Linux内核的开源虚拟化技术。它允许你基于 Linux 或者 Windows 运行多个隔离的客户机虚拟机器。每一个客户机都拥有自己的操作系统和专用的虚拟硬件,例如 CPU(s), 内存, 网络接口和存储。</p>
<p>这篇指南提供了关于如何在 Ubuntu 20.04 上安装和配置 KVM 的指令。我们也会向你展示如何创建虚拟机，它们将被用作不同应用的开发环境。</p>
<h2 id="一、前提条件"><a href="#一、前提条件" class="headerlink" title="一、前提条件"></a>一、前提条件</h2><p>想要运行超过 2GB 内存的客户机，你必须拥有一个 64位主机系统。</p>
<p>在继续安装之前，确保你的 Ubuntu 主机支持 KVM 虚拟化。这个系统必须拥有 支持 VT-x（vmx）的 Intel 处理器 或者支持  AMD-V (svm) 技术的 AMD 处理器。</p>
<p>输入下面的<code>grep</code>命令来看看你的吹气是否支持硬件虚拟化：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep -<span class="title class_">Eoc</span> <span class="string">&#x27;(vmx|svm)&#x27;</span> /proc/cpuinfo</span><br></pre></td></tr></table></figure>

<p>如果你的 CPU 支持硬件虚拟化，这个命令将会打印出大于0的数字，这代表 CPU 核心数目。否则，如果输出为0，它意味着这个 CPU 不支持硬件虚拟化。</p>
<p>在一些机器上，虚拟化技术可能被厂商在 BIOS 中禁用了。</p>
<p>想要检查 VT 是否在 BIOS 中启用了，使用<code>kvm-ok</code>工具，它被包含在<code>cpu-checker</code>软件包。以 root 或者其他有 sudo 权限用户身份运行下面的命令，安装这个软件包：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt update</span><br><span class="line">sudo apt install cpu-checker</span><br></pre></td></tr></table></figure>

<p>一旦安装完成，检查你的系统是否可以运行硬件加速的 KVM 虚拟机：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kvm-ok</span><br></pre></td></tr></table></figure>

<p>如果处理器虚拟化能力没有在 BIOS 中被禁用，命令将会打印出:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">INFO</span>: <span class="regexp">/dev/</span>kvm exists</span><br><span class="line"><span class="variable constant_">KVM</span> acceleration can be used</span><br></pre></td></tr></table></figure>

<p>否则，这个命令将会打印一个失败信息，和一个关于如何启用这个组件的简短消息。启用 AMD-V 或者 VT 技术的过程依赖于你的主板和处理器类型。根据你的主板文档信息来配置你的系统 BIOS。</p>
<h2 id="二、在-Ubuntu-20-04-上安装-KVM"><a href="#二、在-Ubuntu-20-04-上安装-KVM" class="headerlink" title="二、在 Ubuntu 20.04 上安装 KVM"></a>二、在 Ubuntu 20.04 上安装 KVM</h2><p>运行下面的命令安装 KVM，和额外的虚拟化管理软件包：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager</span><br></pre></td></tr></table></figure>

<ul>
<li><code>qemu-kvm</code> - 为 KVM 管理程序提供硬件模拟的软件程序</li>
<li><code>libvirt-daemon-system</code> - 将 libvirt 守护程序作为系统服务运行的配置文件</li>
<li><code>libvirt-clients</code> - 用来管理虚拟化平台的软件</li>
<li><code>bridge-utils</code> - 用来配置网络桥接的<a target="_blank" rel="noopener" href="https://cloud.tencent.com/product/cli?from=20065&from_column=20065">命令行工具</a></li>
<li><code>virtinst</code> - 用来创建虚拟机的命令行工具</li>
<li><code>virt-manager</code> - 提供一个易用的图形界面，并且通过libvirt 支持用于管理虚拟机的命令行工具</li>
</ul>
<p>一旦软件包被安装好，libvirt 守护程序将会自动启动。你可以通过运行下面的命令，验证它：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl is-active libvirtd</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">active</span><br></pre></td></tr></table></figure>

<p>想要创建和管理虚拟机，你需要添加你的用户到“libvirt” 和 “kvm” 用户组。输入：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo usermod -aG libvirt $USER</span><br><span class="line">sudo usermod -aG kvm $USER</span><br></pre></td></tr></table></figure>

<p><code>$USER</code>是一个环境变量，用来记住当前登录用户的名字。</p>
<p>登出，并且重新登录，以便用户组被刷新。</p>
<h2 id="三、网络设置"><a href="#三、网络设置" class="headerlink" title="三、网络设置"></a>三、网络设置</h2><p>在libvirt 安装过程中，一个被称为 “virbr0”的桥接设备默认被创建。这个设备使用 NAT 来连接客户机到外面的世界。</p>
<p>运行 <code>brctl</code> 工具来列出当前的桥接和它们连接的接口：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">brctl show</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bridge name	bridge id		      <span class="variable constant_">STP</span> enabled	interfaces</span><br><span class="line">virbr0		  <span class="number">8000.</span>52540089db3f	yes		      virbr0-nic</span><br></pre></td></tr></table></figure>

<p>“virbr0” 桥接没有添加任何的物理接口。“virbr0-nic”是一个虚拟设备，没有任何流量通过。这个设备唯一的目的就是避免修改“virbr0” 桥接的 MAC 地址。</p>
<p>网络设置适合大部分 Ubuntu 桌面用户，但是有限制。如果你想从外面的本地网络访问客户机，你需要创建一个新的桥接，并且配置它，以便客户机可以通过主机的物理接口连接到外部世界。</p>
<h2 id="四、创建虚拟机"><a href="#四、创建虚拟机" class="headerlink" title="四、创建虚拟机"></a>四、创建虚拟机</h2><p>现在 KVM 在你的 Ubuntu 桌面被安装，让我们一起去创建第一个虚拟机。我们可以从命令行或者使用<code>virt-manager</code>应用。</p>
<p>下载你想要安装的操作系统的 ISO 镜像并且按照下面的步骤来创建你的虚拟机：</p>
<p>01.在搜索栏输入“Virtual Machine Manager”并且点击图标来启动应用。</p>
<p>02.在应用启动后，从顶部菜单点击“File” -&gt; “New Virtual Machine”:</p>
<p>03.一个新窗口将会显示。选择  “Local install media” 并且点击  “Forward” 按钮。</p>
<p>04.提供你的 ISO 镜像路径，并且点击 Forward 按钮。</p>
<p>05.在下一个屏幕，选择虚拟机 VM 的内存和 CPU 设置。点 Forward。</p>
<p>06.下一步，选择“Create a disk image for the virtual machine” 并且选择虚拟机的磁盘空间大小。点击 Forward。</p>
<p>07.输入你的虚拟机名字并且点击 “Finish”。</p>
<p>08.虚拟机启动，并且一个新窗口将会打开：</p>
<p>从这里，你就可以按照屏幕的操作，去完成操作系统的安装。</p>
<p>一旦操作系统被安装好，你可以从<code>virt-manager</code>去访问它，使用 ssh 或者使用 Serial Console 接口。</p>
<h2 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h2><p>我们为你展示如何在 Ubuntu 20.04 系统上安装 KVM。现在你可以创建 Windows 或者 Linux 客户机。想要查找更多关于 KVM 的信息，浏览：<a target="_blank" rel="noopener" href="https://www.linux-kvm.org/page/Documents">KVM 文档页面</a>。</p>
<p>#Ubuntu20.04 网络br0配置<br><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/359864370">https://zhuanlan.zhihu.com/p/359864370</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_38916811/article/details/120792767">https://blog.csdn.net/qq_38916811/article/details/120792767</a><br>#kvm讲课<br><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1qe4y127Hr?p=7&vd_source=ff2618562c52bd8088365be75a8e4c47">https://www.bilibili.com/video/BV1qe4y127Hr?p=7&amp;vd_source=ff2618562c52bd8088365be75a8e4c47</a><br>#Ubuntu20.04 网络配置<br><a target="_blank" rel="noopener" href="https://www.cnblogs.com/Gmiaomiao/p/14550151.html">https://www.cnblogs.com/Gmiaomiao/p/14550151.html</a></p>
<p>#查看虚拟机列表<br>virsh list –all<br>#创建虚拟磁盘<br>qemu-img create -f qcow2 &#x2F;kvm&#x2F;vdisk&#x2F;host91.qcow2 100G #如果是ssd用raw<br>#创建虚拟机<br>virt-install –name&#x3D;host91 <br>–vcpus&#x3D;2 –name&#x3D;2048 –disk&#x3D;&#x2F;kvm&#x2F;vdisk&#x2F;host91.qcow2 <br>–cdrom&#x3D;&#x2F;var&#x2F;lib&#x2F;libvirt&#x2F;images&#x2F;CentOS-7-x86_64-DVD-2003.iso <br>–os-variant&#x3D;rhel7 <br>–noautoconsole –autostart <br>–network bridge:br0 <br>–graphics vnc,listen&#x3D;0.0.0.0,prot&#x3D;5901</p>
<h1 id="Ubuntu20-04安装kvm并使用桥接网络-我的笔记"><a href="#Ubuntu20-04安装kvm并使用桥接网络-我的笔记" class="headerlink" title="Ubuntu20.04安装kvm并使用桥接网络(我的笔记)"></a>Ubuntu20.04安装kvm并使用桥接网络(我的笔记)</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line">1、更新 Ubuntu 22.04</span><br><span class="line">在一切开始前，打开终端并通过如下命令更新本地的软件包索引：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt update</span></span><br><span class="line"></span><br><span class="line">2、检查虚拟化是否开启</span><br><span class="line">在进一步行动之前，首先需要检查你的 CPU 是否支持 KVM 虚拟化，确保你系统中有 VT-x（ vmx）英特尔处理器或 AMD-V（svm）处理器。</span><br><span class="line">你可以通过运行如下命令，如果输出值大于 0，那么虚拟化被启用。否则，虚拟化被禁用，你需要启用它：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">egrep -c <span class="string">&#x27;(vmx|svm)&#x27;</span> /proc/cpuinfo</span></span><br><span class="line">根据上方命令输出，你可以推断出虚拟化功能已经启用，因为输出结果大于 0。如果虚拟化功能没有启用，请确保在系统的 BIOS 设置中启用虚拟化功能。</span><br><span class="line">另外，你可以通过如下命令判断 KVM 虚拟化是否已经在运行：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt install -y cpu-checker</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">kvm-ok</span></span><br><span class="line">运行该命令之前，请确保你已经安装了 cpu-checker 软件包，否则将提示未找到该命令的报错。</span><br><span class="line">直接就在下面，你会得到如何解决这个问题的指示，那就是安装 cpu-checker 包。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">3、在 Ubuntu 22.04 上安装 KVM</span><br><span class="line">随后，通过如下命令在 Ubuntu 22.04 中安装 KVM 以及其他相关虚拟化软件包：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt install -y qemu-kvm virt-manager libvirt-daemon-system virtinst libvirt-clients bridge-utils</span></span><br><span class="line">以下为你解释刚刚安装了哪些软件包：</span><br><span class="line">    qemu-kvm – 一个提供硬件仿真的开源仿真器和虚拟化包</span><br><span class="line">    virt-manager – 一款通过 libvirt 守护进程，基于 QT 的图形界面的虚拟机管理工具</span><br><span class="line">    libvirt-daemon-system – 为运行 libvirt 进程提供必要配置文件的工具</span><br><span class="line">    virtinst – 一套为置备和修改虚拟机提供的命令行工具</span><br><span class="line">    libvirt-clients – 一组客户端的库和API，用于从命令行管理和控制虚拟机和管理程序</span><br><span class="line">    bridge-utils – 一套用于创建和管理桥接设备的工具</span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line"> 4、启用虚拟化守护进程（libvirtd）</span><br><span class="line">在所有软件包安装完毕之后，通过如下命令启用并启动 libvirt 守护进程：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl <span class="built_in">enable</span> --now libvirtd</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl start libvirtd</span></span><br><span class="line">你可以通过如下命令验证该虚拟化守护进程是否已经运行：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl status libvirtd</span></span><br><span class="line">另外，请将当前登录用户加入 kvm 和 libvirt 用户组，以便能够创建和管理虚拟机。</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo usermod -aG kvm <span class="variable">$USER</span></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo usermod -aG libvirt <span class="variable">$USER</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">5、创建网桥（br0）</span><br><span class="line">如果你打算从本机（Ubuntu 22.04）之外访问 KVM 虚拟机，你必须将虚拟机的网卡映射至网桥。virbr0 网桥是 KVM 安装完成后自动创建的，仅做测试用途。</span><br><span class="line">你可以通过如下内容在 /etc/netplan 目录下创建文件 01-netcfg.yaml 来新建网桥：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo vi /etc/netplan/01-netcfg.yaml</span></span><br><span class="line">network:</span><br><span class="line">  ethernets:</span><br><span class="line">    enp0s3:</span><br><span class="line">      dhcp4: false</span><br><span class="line">      dhcp6: false</span><br><span class="line"><span class="meta prompt_">  # </span><span class="language-bash">add configuration <span class="keyword">for</span> bridge interface</span></span><br><span class="line">  bridges:</span><br><span class="line">    br0:</span><br><span class="line">      interfaces: [enp0s3]</span><br><span class="line">      dhcp4: false</span><br><span class="line">      addresses: [192.168.1.162/24]</span><br><span class="line">      macaddress: 08:00:27:4b:1d:45</span><br><span class="line">      routes:</span><br><span class="line">        - to: default</span><br><span class="line">          via: 192.168.1.1</span><br><span class="line">          metric: 100</span><br><span class="line">      nameservers:</span><br><span class="line">        addresses: [4.2.2.2]</span><br><span class="line">      parameters:</span><br><span class="line">        stp: false</span><br><span class="line">      dhcp6: false</span><br><span class="line">  version: 2</span><br><span class="line">保存并退出文件。 </span><br><span class="line">--或者用我下面这个:</span><br><span class="line">pc@pc:/etc/netplan$ cat 00-installer-config.yaml </span><br><span class="line">network:</span><br><span class="line">  ethernets:</span><br><span class="line">    ens133:</span><br><span class="line">      dhcp4: false</span><br><span class="line">      dhcp6: false</span><br><span class="line">  bridges:</span><br><span class="line">    br0:</span><br><span class="line">      interfaces: [ ens133 ]</span><br><span class="line">      addresses: [192.168.1.100/24]</span><br><span class="line">      gateway4: 192.168.1.1</span><br><span class="line">      mtu: 1500</span><br><span class="line">      nameservers:</span><br><span class="line">        addresses: [8.8.8.8,8.8.4.4]</span><br><span class="line">      dhcp4: no</span><br><span class="line">      dhcp6: no</span><br><span class="line">  version: 2</span><br><span class="line"></span><br><span class="line">pc@pc:/etc/netplan$</span><br><span class="line">--说明: 物理网卡上面不要配置ip,配置在虚拟br0上</span><br><span class="line">pc@pc:/etc/netplan$ ip a</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</span><br><span class="line">    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1/8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1/128 scope host </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens133: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000</span><br><span class="line">    link/ether 00:e0:4c:cd:24:71 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    altname enp3s0</span><br><span class="line">5: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000</span><br><span class="line">    link/ether 00:e0:4c:cd:24:71 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute br0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 2409:8a4c:63a:d680:ebb1:953c:d6bb:b8da/64 scope global temporary dynamic </span><br><span class="line">       valid_lft 193986sec preferred_lft 85166sec</span><br><span class="line">    inet6 2409:8a4c:63a:d680:2e0:4cff:fecd:2471/64 scope global dynamic mngtmpaddr noprefixroute </span><br><span class="line">       valid_lft 193986sec preferred_lft 107586sec</span><br><span class="line">    inet6 fe80::2e0:4cff:fecd:2471/64 scope link noprefixroute </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">6: vnet0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000</span><br><span class="line">    link/ether fe:54:00:f0:04:66 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet6 fe80::fc54:ff:fef0:466/64 scope link </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">pc@pc:/etc/netplan$ </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo netplan try</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo netplan apply</span></span><br><span class="line">你可以通过如下 ip 命令，验证网桥 br0：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">ip add show</span></span><br><span class="line">pc@pc:/etc/netplan$ route</span><br><span class="line">内核 IP 路由表</span><br><span class="line">目标            网关            子网掩码        标志  跃点   引用  使用 接口</span><br><span class="line">default         _gateway        0.0.0.0         UG    425    0        0 br0</span><br><span class="line">link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens133</span><br><span class="line">192.168.1.0     0.0.0.0         255.255.255.0   U     425    0        0 br0</span><br><span class="line">pc@pc:/etc/netplan$</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">virt-manager</span></span><br><span class="line"></span><br><span class="line">6、启动 KVM 虚拟机管理器</span><br><span class="line">当 KVM 安装完成后，你可以使用图形管理工具 virt-manager 创建虚拟机。你可以在 GNOME 搜索工具中搜索 Virtual Machine Manager 以启动。</span><br><span class="line">下面就算图形化安装,安装完成后也可以换桥接模式,通过详细界面选择</span><br><span class="line"></span><br><span class="line">7. VM是centos系统,配置静态IP</span><br><span class="line">[root@anonymous ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 </span><br><span class="line">TYPE=Ethernet</span><br><span class="line">PROXY_METHOD=none</span><br><span class="line">BROWSER_ONLY=no</span><br><span class="line">BOOTPROTO=static</span><br><span class="line">DEFROUTE=yes</span><br><span class="line">IPV4_FAILURE_FATAL=no</span><br><span class="line">IPV6INIT=yes</span><br><span class="line">IPV6_AUTOCONF=yes</span><br><span class="line">IPV6_DEFROUTE=yes</span><br><span class="line">IPV6_FAILURE_FATAL=no</span><br><span class="line">IPV6_ADDR_GEN_MODE=stable-privacy</span><br><span class="line">NAME=eth0</span><br><span class="line">UUID=aa98c4ed-0cd5-48e3-ab58-32682358cb8a</span><br><span class="line">DEVICE=eth0</span><br><span class="line">ONBOOT=yes</span><br><span class="line"></span><br><span class="line">IPADDR=192.168.1.11</span><br><span class="line">NETMASK=255.255.255.0</span><br><span class="line">DNS=1114.114.114.114</span><br><span class="line">GATEWAY=192.168.1.1</span><br><span class="line"></span><br><span class="line">[root@anonymous ~]#</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">小结: </span><br><span class="line">1, ubuntu使用了netplan管理网络</span><br><span class="line">2, 宿主机添加好桥接网卡后,能通过桥接网卡上网后在安装vm</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/04/21/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20-04%E4%B8%8A%E5%AE%89%E8%A3%85KVM/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/KVM/" rel="tag">KVM</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ubuntu/" rel="tag">Ubuntu</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/04/23/Keepalive%E9%AB%98%E5%8F%AF%E7%94%A8GBase8s/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Keepalive高可用GBase8s
          
        </div>
      </a>
    
    
      <a href="/2023/04/20/python%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">python人工智能学习环境搭建</div>
      </a>
    
  </nav>

  
   
  
    
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>